#include <cstdint>
#include "triu_custom_tiling.h"
#include "tiling/platform/platform_ascendc.h"

const uint32_t BlockSize = 32;
const uint64_t ub_Sharing_Num = 2;


void GenerateTilingData(uint8_t *tilingBuf)
{
    TriuCustomTilingData *tiling = reinterpret_cast<TriuCustomTilingData *>(tilingBuf);
    
    uint32_t type_Size = sizeof(uint16_t);
    uint32_t align_Num = BlockSize / type_Size;
    uint32_t total_Length_Aligned = (tiling->matrixNum * tiling->rowLength * tiling->columnLength + align_Num - 1) / align_Num * align_Num;
    tiling->totalLengthAligned = total_Length_Aligned;

    uint64_t ub_size=196352; // Default UB size, can be adjusted based on the platform
    uint64_t ub_length = ((ub_size / type_Size / ub_Sharing_Num) / align_Num * align_Num) - align_Num;
    uint64_t loop_Cnt = (total_Length_Aligned + ub_length - 1) / ub_length;
    uint64_t full_Tile_Length = ub_length;
    uint64_t last_Tile_Length = (total_Length_Aligned - full_Tile_Length * (loop_Cnt - 1) + align_Num - 1) / align_Num * align_Num;
    if (loop_Cnt == 1){ full_Tile_Length = 0; }
    tiling->fullTileLength = full_Tile_Length;
    tiling->lastTileLength = last_Tile_Length;
}